﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;

namespace Portal.CMS
{
    public class UpdateDocument : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UpdateDocument";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Cập nhật tài liệu văn bản");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateDocument));
            string DocumentId = System.Web.HttpContext.Current.Request["DocumentId"];
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Document.js") +
                Environments.GetFormDialog(
                 "<div class=cssFormContent id=divFormContent>" +
                    GetHtml(DocumentId) +
                 "</div>", ResourceBLL.GetText(WebPartId, "UpdateDocumentFormTitle", "Cập nhật Tài liệu văn bản"));


            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string DocumentId)
        {
            string RetHtml = "";

            DocumentCls DocumentCls = new DocumentBLL().CreateModel(Environments.ActiveLanguage, DocumentId);
            if (DocumentCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }

            string SelectLanguageText =
             "<select id=divSelectLanguage onchange=javascript:DetectDocumentDescription('" + DocumentId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";
            RetHtml +=
                             "      <table>" +
                              "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddNewsLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateDocumentCaption", "Tài liệu văn bản") + "</td>" +
                             "              <td><input id=txtDocument class=cssTextBoxDialog value=\"" + DocumentCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateDocumentDescription", "Mô tả tài liệu") + "</td>" +
                             "              <td><textarea id=txtDescription class=cssTextBoxDialog>" + DocumentCls.Description + "</textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "JumpUrl", "Url") + ":</td>" +
                             "              <td><input class=cssTextBoxDialog  id=txtUrl value=\""+DocumentCls.JumpUrl+"\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateDocument('" + DocumentId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateDocumentSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateDocumentCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>";


            return RetHtml;
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateDocument(string ActiveLanguage, string DocumentId, string DocumentName, string Description, string JumpUrl)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (DocumentName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId, "EnterName", "Nhập tên đối tượng"));
                new DocumentBLL().UpdateDocument(null, ActiveLanguage, DocumentId, DocumentName, Description,JumpUrl);
                return ResourceBLL.GetText(WebPartId, "MessageUpdateOk", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerGetDescription(string ActiveLanguage, string DocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                DocumentCls DocumentCls = new DocumentBLL().CreateModel(ActiveLanguage, DocumentId);
                return DocumentCls.Description;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string GetAddDocumentItemUrl(string DocumentId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadDocument().WebPartId),
                        new ParamCls("DocumentId",DocumentId)
                    });
            return AppendUrl;
        }
    }
}